#include "NxModelServiceRecord.h"
#include "../utils/NxHttp.h"
#include "NxDataFactory.h"
#include <iostream>
#include <time.h>


const char* NJGIS::SERVICE::NjModelServiceRecord::getID()
{
	return this->_oid.c_str();
}

const char* NJGIS::SERVICE::NjModelServiceRecord::getModelServiceID()
{
	return this->_msid.c_str();
}

const char* NJGIS::SERVICE::NjModelServiceRecord::getStartDatetime()
{
	return this->_datetime.c_str();
}

int NJGIS::SERVICE::NjModelServiceRecord::getTimeSpan()
{
	return this->_span;
}

NJGIS::SERVICE::INjDataConfiguration* NJGIS::SERVICE::NjModelServiceRecord::getInputData()
{
	return this->_input;
}

NJGIS::SERVICE::INjDataConfiguration* NJGIS::SERVICE::NjModelServiceRecord::getOutputData()
{
	return this->_output;
}

NJGIS::SERVICE::NjRecordStatus NJGIS::SERVICE::NjModelServiceRecord::getStatus()
{
	return this->_status;
}

const char* NJGIS::SERVICE::NjModelServiceRecord::getRunningInfo_Standout()
{
	return this->_stdout.c_str();
}

const char* NJGIS::SERVICE::NjModelServiceRecord::getRunningInfo_Standerr()
{
	return this->_stderr.c_str();
}

const char* NJGIS::SERVICE::NjModelServiceRecord::getRunningInfo_Invokeerr()
{
	return this->_invkerr.c_str();
}

std::vector<NJGIS::SERVICE::INjRunningLog*>* NJGIS::SERVICE::NjModelServiceRecord::getLogs()
{
	return this->_list_log;
}

int NJGIS::SERVICE::NjModelServiceRecord::refresh()
{

	Json::Value jResponse;
	std::string url;
	this->getBaseUrl(url);
	url += "modelserrun/json/";
	url += std::string(this->getID());
	NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(),jResponse);

	//std::cout<< jResponse["result"].asString() <<std::endl;

    if(jResponse["result"].asString() == "suc"){
		Json::Value jMsr = jResponse["data"];
		NJGIS::SERVICE::INjDataConfiguration* pInput = NJGIS::SERVICE::NjDataFactory::createDataConfigByJSON(jMsr["msr_input"]);
		NJGIS::SERVICE::INjDataConfiguration* pOutput = NJGIS::SERVICE::NjDataFactory::createDataConfigByJSON(jMsr["msr_output"]);

		//update information
		this->_input = pInput;
		this->_output = pOutput;
		this->_span = jMsr["msr_span"].asDouble();
		NJGIS::SERVICE::NjRecordStatus restatus;
		if(jMsr["msr_status"].asInt() == 1){
			restatus = NJGIS::SERVICE::NjRecordStatus::MSRS_FINISHED;
		}else if(jMsr["msr_status"].asInt() == 0){
			restatus = NJGIS::SERVICE::NjRecordStatus::MSRS_UNFINISHED;
		}else{
			restatus = NJGIS::SERVICE::NjRecordStatus::MSRS_ERROR;
		}
		this->_status = restatus;

		this->_invkerr = jMsr["msr_runninginfo"]["InvokeErr"].asString().c_str();

		this->_stdout = jMsr["msr_runninginfo"]["StdOut"].asString().c_str();

		this->_stderr = jMsr["msr_runninginfo"]["StdErr"].asString().c_str();

		//update log information
		  Json::Value jLogs = jMsr["msr_logs"];
		  for(int i =0 ; i < jLogs.size(); i++){
			  this->_list_log->push_back(NJRunningLogFactory::createRunningLogByJSON(jLogs[i]));
		  }
		} else{
		  return -1;
		}		   
			
	return 1;
}

void NJGIS::SERVICE::NjModelServiceRecord::wait4Finished()
{

}

const char* NJGIS::SERVICE::NjModelServiceRecord::getGUID()
{
	return this->_guid.c_str();
}
